$(function() {
	var RoleListGrid = $("#RoleListGrid");
	var rolePermissionPanel = $("#rolePermissionPanel");
	var rolePermissionTree = $("#rolePermissionTree");
	var currentRow;
	var roleGridAction = $("#roleGridAction");
	// 初始化

	RoleListGrid.datagrid({
		url :'role/manager/list',
		fit : true,
		rownumbers : true,
		pagination : true,// 是否显示分页栏
		border : false,
		singleSelect : true,
		fitColumns : true,// 自适应大小
		columns : [ [
				{
					field : 'roleName',
					title : '角色名',
					align : 'center',
					width : 100
				},
				{
					field : 'roleKey',
					title : '角色标识',
					width : 100
				},
				{
					field : 'roleDescribe',
					title : '描述',
					width : 200
				},
				{
					field : 'enable',
					title : '状态',
					width : 100,
					align : 'center',
					formatter : function(val) {
						return val == true ? '可用' : '不可用';
					}
				},
				{
					field : 'ac',
					title : '操作',
					width : 150,
					align : 'center',
					formatter : function(value, row, index) {
						return roleGridAction.children("a.ctr").attr('data-id',
								row.roleId).attr('data-index', index)
								.end().html();
					}
				} ] ],
		toolbar : "#roleGridToolbar",
		onSelect : function(index, row) {
			// 记录当前选中行

			currentRow = row;

			if (row.roleName === "超级管理员") {
				rolePermissionPanel.panel("setTitle", "超级管理员权限");

				// 取消以前已经选中的项目

				$.each(rolePermissionTree.tree("getRoots"), function() {
					rolePermissionTree.tree("check", this.target)
				});
			} else {
				rolePermissionPanel.panel("setTitle", "为[" + row.roleName
						+ "]分配权限");

				// 取消以前已经选中的项目

				$.each(rolePermissionTree.tree("getChecked"), function() {
					rolePermissionTree.tree("uncheck", this.target)
				});

				// 加载当前选择角色的已有权限

				$.get("role/manager/role/resource/" + row.roleId,
						function(data) {
							$.each(data, function() {
								var node = rolePermissionTree.tree('find',
										this.resourceId);
								// 只当前权限是树的叶子节点时才执行节点的check方法进行选择

								if (rolePermissionTree.tree('isLeaf',
										node.target)) {
									rolePermissionTree.tree('check',
											node.target)
								}
							});
						});
			}

		},
		onLoadSuccess : function() {
			$("#add-role").parent().parent().css("height", "36");
			$("#add-role").css("padding", "5");

		}

	});

	rolePermissionTree.tree({
		url : 'role/manager/role/resource/tree',
		checkbox : true
	});

	var roleName = $("#roleName");

	// 检索按钮事件

	$("#roleSearch").on('click', function() {
		RoleListGrid.datagrid("load", {
			roleName : "%" + roleName.val() + "%"
		});
	});

	var form = $("#roleSearchForm");

	$("#roleReset").on('click', function() {
		form.form('clear');
		// 清除查询参数

		RoleListGrid.datagrid("load", {});
	});

	var gridPanel = RoleListGrid.datagrid("getPanel");

	// 给操作按钮绑定事件

	gridPanel.on("click", "a.edit", function() {
		var id = this.dataset.id;
		formDialog(id);
	}).on("click", "a.delete", function() {
		var id = this.dataset.id;
		$.messager.confirm("提示", "是否删除？", function(r) {
			if (r) {
				$.get("role/manager/delete?id=" + id, null, function(rs) {
					if (rs.success) {
						RoleListGrid.datagrid("reload");
					} else {
						$.messager.alert("提示", rs.msg);
					}
				});
			}
		})
	}).on("click", "a.create", function() {
		formDialog();
	})
	/**
	 * 
	 * 表单窗口
	 * 
	 */
	function formDialog(id) {
		var dialog = $("<div/>").dialog(
				{
					title : (id ? '编辑' : '添加') + '角色',
					href : '/role/manager/' + (id ? 'load?id=' + id : 'form'),
					width : 480,
					height :350,
					onClose : function() {
						// 销毁窗口

						$(this).dialog("destroy");
					},
					buttons : [ {
						iconCls : 'icon-save',
						text : '保存',
						handler : function() {
							var roleForm = $("#roleForm");
							if (roleForm.form("validate")) {
								$.post("role/manager/"
										+ (id ? 'update' : 'save'), roleForm
										.serialize(), function(rs) {
									if (rs.success) {
										RoleListGrid.datagrid("reload");
										dialog.dialog('close');
									} else {
										$.messager.alert("提示", rs.msg);
									}
								});
							}
						}
					} ]
				});
	}

	// 权限保存的按钮事件

	$("#rolePermissionSave").on('click',function() {
				if (currentRow) {
					if (currentRow.roleName != "超级管理员") {

						// 获取打钩和实心的节点

						var nodes = rolePermissionTree.tree("getChecked", [
								"checked", "indeterminate" ]);
						var resourceIds = [];
						$.each(nodes, function() {
							resourceIds.push(this.id);
						});

						var params = "roleId=" + currentRow.roleId
								+ "&resourceId="
								+ resourceIds.join("&resourceId=");

						$.post("role/manager/resource/save", params, function(
								resp) {
							if (resp.success) {
								$.messager.alert("提示", "授权成功！");
							} else {
								$.messager.alert("提示", "授权失败！");
							}
						})

					} else {
						$.messager.alert("提示", "超级管理员无法被授权！");
					}

				}
			});
});